var BasmallahMainIndex;

//gunakan self invocation function
(function($){

BasmallahMainIndex = {
	
	//dom element
	dom:{
		mainContent:null,
		indexContent:null,
		contentBottom:null
	},
	
	vr:{
		lastOffset:0,
		locationHash:window.pageYOffset,
		lastLocation:0
	},
	
	//tambahkan semua fungsi disini
	fn:{
		
		loadYayasan:function(){
			var url = basmallah_home + "/yayasan/dir/get_more/0";
			$.ajax({url:url,
					dataType:"json",
					beforeSend:function(){
						
					},
					success:function(data){
				if(data.result!=false){
					
					var current = null;
					for(i=0;i<data.result.length;i++){
						current = data.result[i];

						//put yayasan ke layar
						BasmallahMainIndex.fn.putYayasan(current);							
					}	
					
					BasmallahMainIndex.vr.lastOffset = data.next_offset;
						
				}
			}});
		},

		loadMoreYayasan:function(){
			var url = basmallah_home + "/yayasan/dir/get_more/" + BasmallahMainIndex.vr.lastOffset + "/" + 3;
			$.getJSON(url,function(data){
				if(data.result!=false){
					
					var current = null;
					for(i=0;i<data.result.length;i++){
						current = data.result[i];
						
						//put yayasan ke layar
						BasmallahMainIndex.fn.putYayasan(current);
					}	
					console.log(data.next_offset);
					BasmallahMainIndex.vr.lastOffset = data.next_offset;					
				}
			});
		},
		
		createMoreButton:function(data)
		{
			var xdiv = $("<div id='"+ data.nama_yayasan +"'>More</div");
			xdiv.addClass("more_band");
			return xdiv;		
		},
		
		createYayasan:function(data){
		
			var xdiv = $("<div></div");
			xdiv.addClass("yayasan_tile");
			xdiv.css("display","none");
			xdiv.append("<div class='nama_yayasan'>"+ data.nama_yayasan +"</div>");								
			xdiv.append("<div class='alamat_yayasan'>"+ data.alamat +"</div>");
			xdiv.append("<div class='foto_yayasan'></div>");
			return xdiv;
		},
		
		putElement:function(y){
			BasmallahMainIndex.dom.indexContent.append(y);
			y.fadeIn("slow");		
		},
		
		putYayasan:function(data){
			var y = BasmallahMainIndex.fn.createYayasan(data);
			BasmallahMainIndex.fn.putElement(y);
		}	
	},
	
	//pesan semua variabel yang membutuhkan element 
	//di function ini
	prepareDom:function()
	{
		this.dom = {
			mainContent : $("#content"),
			indexContent : $("#indexContent"),
			contentBottom : $("#contentBottom"),
			a_pencarian_lengkap :$("#a_pencarian_lengkap"),
			div_pencarian : $("#pencarian"),
			pencarian_detail : $("#pencarian_detail")	
		}
	},
	
	//view
	constructView:function(){
	},
	
	//handle every event disni
	bindEvent:function()
	{
		this.dom.indexContent.delegate("div","mouseOver",function(){
			//alert("test");
		});

		this.dom.indexContent.delegate(".more_band","click",function(){
			BasmallahMainIndex.fn.loadMoreYayasan();
		});
				
		$(window).scroll(function(){
			var win = $(window);
			var theight = win.height() + win.scrollTop();
			var docHeight = $(document).height();
			var t = parseInt(docHeight) - parseInt(win.scrollTop());
			
			console.log(t);
			if(t<820)
			{
				BasmallahMainIndex.fn.loadMoreYayasan();
			}
			
			BasmallahMainIndex.vr.lastLocation = $(document).scrollTop();			
		});
		
		//toggle show hide detail form
		this.dom.a_pencarian_lengkap.click(function(){
			//TODO
		});
	},
	
	run:function(){
       this.prepareDom();
       this.constructView();
       this.bindEvent();	
	   
	   //load data from yayaysan
	   this.fn.loadYayasan();
	}
};


$(function () {
    //run
    BasmallahMainIndex.run();
});

})(jQuery);